home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magnum One
/
Magnum One (Mid-American Digital) (Disc Manufacturing).iso
/
d20
/
vp2_409e.szh
/
STATS.C
< prev
next >
Wrap
C/C++ Source or Header
|
1991-06-27
|
14KB
|
525 lines
/*
The Conference Mail System
Statistics and Logging Module
This module was originally written by Graham Stair
Sysop of FidoNet node 3:711/409
26th February 1991
*/
#define RELEASE "1a"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <malloc.h>
#include <sys\types.h>
#include <sys\stat.h>
#include "types.h"
#if OS2
#define INCL_DOSFILEMGR
#include <os2.h>
#define FSINFOBUF FSInfoBuf
#define AVAILCLUSTERS cUnitAvail
#define SECTPERCLUSTER cSectorUnit
#define BYTESPERSECTOR cbSector
#else
#include <dos.h>
#define FSINFOBUF DiskInfo
#define AVAILCLUSTERS avail_clusters
#define SECTPERCLUSTER sectors_per_cluster
#define BYTESPERSECTOR bytes_per_sector
#endif
/* -- struct's and tyepdef's --------------------------------------------- */
struct _packet_header
{
int OrigNode;
int DestNode;
int CreateYear;
int CreateMonth;
int CreateDay;
int CreateHour;
int CreateMinute;
int CreateSecond;
int Baud;
int PacketType;
int OrigNet;
int DestNet;
char ProductCode;
char SerialNum;
char Password[8];
int OrigZone;
int DestZone;
char Filler[20];
};
/* -- Global Variables --------------------------------------------------- */
int mail_msgs = 0;
long mail_size = 0;
int security = 0;
extern char *REV;
/* -- Local Prototypes --------------------------------------------------- */
void LogDateStamp(char *DateStamp);
char LogOpen(void);
char LogClose(void);
/* -- Local Variables ---------------------------------------------------- */
static char *LogEntry = NULL;
static signed char LogOpened = -1;
static FILE *LogHandle = NULL;
static char *LogName = NULL;
static unsigned int TotMsgs = 0;
static unsigned int TotDupes = 0;
static unsigned int TotSec = 0;
static unsigned int TotSent = 0;
static time_t BeginTime;
static char Marker[60];
static int MarkerLen;
static char *Message = NULL;
static char Type[9];
/* ----------------------------------------------------------------------- */
void LogIt(int status, char *message)
{
char Date[16];
int MessageLen;
char StatusCharacter;
switch (status)
{
case 1:
StatusCharacter = '!'; /* Error */
break;
case 2:
StatusCharacter = '#';
break;
case 3:
StatusCharacter = '*';
break;
case 4:
StatusCharacter = '+';
break;
default:
StatusCharacter = ' ';
break;
}
LogDateStamp(Date);
MessageLen = strlen(message);
for ( ; MessageLen > 0; MessageLen--)
if (message[MessageLen] == '\n')
{
message[MessageLen] = ' ';
break;
}
#if OS2
sprintf(LogEntry, "%c %s VP2 %s\n", StatusCharacter, Date, message);
#else
sprintf(LogEntry, "%c %s VP %s\n", StatusCharacter, Date, message);
#endif
fwrite(LogEntry, strlen(LogEntry), 1, LogHandle);
if (MessageLen > 0)
message[MessageLen] = '\n';
}
/* ----------------------------------------------------------------------- */
char *LogMessage(int status, char *fmt, ...)
{
va_list arg;
if (Message == NULL)
if ((Message = (char *) malloc ( (size_t) (260) )) == NULL)
{
printf("Problem getting 260 bytes for Message\n");
exit(1);
}
va_start (arg, fmt);
vsprintf (Message, fmt, arg);
va_end (arg);
LogIt(status, Message);
return(Message);
}
/* ----------------------------------------------------------------------- */
void LogDateStamp(char *DateStamp)
{
time_t current_time_sec;
struct tm *current_time_str;
char months[12][4] = { "Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec" };
time(¤t_time_sec);
current_time_str = localtime(¤t_time_sec);
sprintf(DateStamp, "%02d %3s %02d:%02d:%02d",
current_time_str->tm_mday,
months[current_time_str->tm_mon],
current_time_str->tm_hour,
current_time_str->tm_min,
current_time_str->tm_sec);
}
/* ----------------------------------------------------------------------- */
unsigned char LogOpen()
{
LogName = getenv("VP2.LOG");
if (LogName == NULL)
{
LogName = getenv("VP.LOG");
if (LogName == NULL)
LogName = getenv("BBS.LOG");
}
if (LogName == NULL)
return(99); /* Log NOT wanted */
if ((LogHandle = fopen(LogName, "a")) == NULL)
fprintf(stderr, "\aERROR, couldn't open log file '%s'\n\r\n\r", LogName);
if (Message == NULL)
if ((Message = (char *) malloc ( (size_t) (260) )) == NULL)
{
printf("Problem getting 260 bytes for Message\n");
exit(1);
}
if (LogEntry == NULL)
if ((LogEntry = (char *) malloc ( (size_t) (260) )) == NULL)
{
printf("Problem getting 260 bytes for LogEntry\n");
exit(1);
}
return(1);
}
/* ----------------------------------------------------------------------- */
unsigned char LogClose()
{
return(-1);
}
/* ----------------------------------------------------------------------- */
void LogBegin(char *type)
{
long DiskSize = 0;
unsigned int Drive;
unsigned int i;
char buff1[20], buff2[20];
#if OS2
long DriveMap;
FSALLOCATE FSINFOBUF;
#else
struct diskfree_t FSINFOBUF;
#endif
if (LogOpened == -1) /* Logging not installed yet */
LogOpened = LogOpen();
if (LogOpened == 99) /* No logging wanted */
return;
/* OK, Logging wanted, file found and opened. Let's tell the world */
printf ("Modified for Logging and Statistics by Graham Stair (Release %s)\n", RELEASE);
printf ("Logging active to file [%s]\n\n", LogName);
#if OS2
DosQCurDisk(&Drive, &DriveMap);
if (DosQFSInfo( Drive, 1, (PBYTE)&FSINFOBUF, sizeof(FSINFOBUF)) == 0)
#else
_dos_getdrive(&Drive);
if (_dos_getdiskfree( Drive, &FSINFOBUF) == 0)
#endif
DiskSize = ((long)FSINFOBUF.AVAILCLUSTERS
* (long)FSINFOBUF.SECTPERCLUSTER
* (long)FSINFOBUF.BYTESPERSECTOR/1024L);
time(&BeginTime);
sscanf (REV, "$%s %s", buff1, buff2);
for (i = 0; i < sizeof(Marker); i++)
Marker[i] = '*';
Marker[sizeof(Marker)-1] = 0;
/* Save 'type' for LogExit's use */
strcpy(Type, type);
sprintf(Message, "%s Begin disk[%c]=%lukb free; rel=%s(%s);",
Type, Drive+64, DiskSize, buff2, RELEASE);
strncat(Message, Marker, (56 - strlen(Message)) );
LogIt(5, Message);
}
/* ----------------------------------------------------------------------- */
void LogExit(int rc)
{
long DiskSize = 0;
unsigned int Drive;
time_t EndTime;
double et;
#if OS2
long DriveMap;
FSALLOCATE FSINFOBUF;
#else
struct diskfree_t FSINFOBUF;
#endif
if (LogOpened == 99) /* No logging wanted */
exit(rc);
#if OS2
DosQCurDisk(&Drive, &DriveMap);
if (DosQFSInfo( Drive, 1, (PBYTE)&FSINFOBUF, sizeof(FSINFOBUF)) == 0)
#else
_dos_getdrive(&Drive);
if (_dos_getdiskfree( Drive, &FSINFOBUF) == 0)
#endif
DiskSize = ((long)FSINFOBUF.AVAILCLUSTERS
* (long)FSINFOBUF.SECTPERCLUSTER
* (long)FSINFOBUF.BYTESPERSECTOR/1024L);
time(&EndTime);
et = difftime(EndTime, BeginTime);
if ( (TotMsgs + TotDupes + TotSec) > 0)
{
sprintf(Message, "I-Summary; msgs=%u; dupes=%u; sec=%u; msgs/sec=%0.1f;",
TotMsgs, TotDupes, TotSec,
(double) ((TotMsgs+TotDupes+TotSec)/(et < 1 ? (double) 1 : et)) );
LogIt(3, Message);
}
if ( TotSent > 0)
{
sprintf(Message, "E-Summary; msgs=%u; msgs/sec=%0.1f;",
TotSent, (double) ((TotSent)/(et < 1 ? (double) 1 : et)) );
LogIt(3, Message);
}
sprintf(Message, "%s End disk[%c]=%lukb free; rc=%u; et=%0.f;",
Type, Drive+64, DiskSize, rc, et);
LogIt(5, Message);
LogOpened = LogClose();
exit(rc);
}
/* ----------------------------------------------------------------------- */
void LogArchive(char *fn, int net, int node)
{
struct stat FnStats;
struct tm *date;
unsigned int i;
if (LogOpened == 99) /* No logging wanted */
return;
for (i = 0; i < sizeof(Marker); i++)
Marker[i] = '=';
Marker[sizeof(Marker)-1] = 0;
sprintf(Message, "Archive=[%s] ", strlwr(fn));
strncat(Message, Marker, (56 - strlen(Message)) );
LogIt(4, Message);
stat(fn, &FnStats);
date = localtime(&FnStats.st_mtime);
sprintf(Message, "Archive date=%u/%02u/%02u %02u:%02u:%02u; size=%lub;",
date->tm_year, date->tm_mon+1, date->tm_mday,
date->tm_hour, date->tm_min, date->tm_sec,
FnStats.st_size);
LogIt(4, Message);
sprintf(Message, "From=%d/%d;", net, node);
LogIt(4, Message);
}
/* ----------------------------------------------------------------------- */
void LogAreas(AREAS_PTR areas[], int tot_areas)
{
int i;
if (LogOpened == 99) /* No logging wanted */
return;
if (security != 0)
{
sprintf(Message, "Area=$Security$; added=%u;", security);
LogIt(1, Message);
TotSec += security;
security = 0;
}
if (mail_msgs != 0)
{
sprintf(Message, "Area=$NetMail$; added=%u; size=%lub;", mail_msgs, mail_size);
LogIt(4, Message);
TotMsgs += mail_msgs;
mail_msgs = 0;
mail_size = 0;
}
for (i = 0; i < tot_areas; i++)
if ((areas[i]->msgs_proc != 0)
|| (areas[i]->msgs_sec != 0)
|| (areas[i]->msg_dupes != 0) )
{
sprintf(Message, "Area=%s; added=%u; size=%lub; dupes=%u; sec=%u;",
areas[i]->area_name,
areas[i]->msgs_proc, areas[i]->msgs_size,
areas[i]->msg_dupes, areas[i]->msgs_sec);
if ((areas[i]->msg_dupes != 0)
|| (areas[i]->msgs_sec != 0) )
LogIt(1, Message);
else
LogIt(4, Message);
TotMsgs += areas[i]->msgs_proc;
TotDupes += areas[i]->msg_dupes;
TotSec += areas[i]->msgs_sec;
areas[i]->msgs_proc = 0;
areas[i]->msgs_size = 0;
areas[i]->msg_dupes = 0;
areas[i]->msgs_sec = 0;
}
}
/* ----------------------------------------------------------------------- */
void LogScanArea(char *Area, unsigned int HighMsg, unsigned int LastMsg)
{
if (LogOpened == 99) /* No logging wanted */
return;
sprintf(Message, "Area=%s; high=%u; last=%u;", Area, HighMsg, LastMsg);
LogIt(4, Message);
}
/* ----------------------------------------------------------------------- */
void LogScanNodes(AREAS_PTR area)
{
int i;
if (LogOpened == 99) /* No logging wanted */
return;
for (i = 0; i < area->num_nodes; i++)
if (area->msgs_scanned[i] > 0)
{
sprintf(Message, "Sent=%u; to=%d:%d/%d;",
area->msgs_scanned[i],
area->zone[i], area->net[i], area->node[i]);
TotSent += area->msgs_scanned[i];
LogIt(4, Message);
}
}
/* ----------------------------------------------------------------------- */
void LogPacket (char *pktname)
{
struct stat FnStats;
struct _packet_header PktHeader;
FILE *PktHandle;
unsigned int i;
if (LogOpened == 99) /* No logging wanted */
return;
for (i = 0; i < sizeof(Marker); i++)
Marker[i] = '-' ;
Marker[sizeof(Marker)-1] = 0;
sprintf(Message, "Packet=[%s] ", strlwr(pktname));
strncat(Message, Marker, (56 - strlen(Message)) );
LogIt(4, Message);
if ((PktHandle = fopen(pktname, "rb")) == NULL)
{
printf("Problem opening packet '%s\n", pktname);
exit(1);
}
if (fread(&PktHeader, sizeof(PktHeader), 1, PktHandle) != 1)
{
printf("Problem reading header of packet '%s\n", pktname);
exit(1);
}
fclose(PktHandle);
stat(pktname, &FnStats);
sprintf(Message, "Packed=%d/%02d/%02d %02d:%02d:%02d; size=%lub;",
PktHeader.CreateYear, PktHeader.CreateMonth+1, PktHeader.CreateDay,
PktHeader.CreateHour, PktHeader.CreateMinute, PktHeader.CreateSecond,
FnStats.st_size);
LogIt(4, Message);
sprintf(Message, "From=%d:%d/%d; to=%d:%d/%d; pcode=0x%02x; serial#=0x%02x;",
PktHeader.OrigZone, PktHeader.OrigNet, PktHeader.OrigNode,
PktHeader.DestZone, PktHeader.DestNet, PktHeader.DestNode,
PktHeader.ProductCode, PktHeader.SerialNum);
LogIt(4, Message);
}